package com.zjg.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.common.entity.A_adminer;
import com.common.entity.A_opinion;
import com.common.entity.A_reply;
import com.common.utils.StringUtils;
import com.zjg.dao.OpinionDao;
import com.zjg.dao.ReplyDao;
import com.zjg.dao.impl.OpinionDaoImpl;
import com.zjg.dao.impl.ReplyDaoImpl;

public class ReplyServlet extends HttpServlet {
	private ReplyDao replyDao=new ReplyDaoImpl();
	private OpinionDao opinionDao=new OpinionDaoImpl();
	
	
	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String opr=request.getParameter("opr");
		
		if("add".equals(opr)){
			add(request, resp);
		}else if("del".equals(opr)){
			delete(request, resp);
		}else{
			list(request, resp);
		}
	}
	
	
	private void list(HttpServletRequest request, HttpServletResponse resp)
			throws ServletException, IOException {
		Integer opinion_id=StringUtils.str2Integer(request.getParameter("opinion_id"), null);
		Map<String, Object> map=new HashMap<String, Object>();
		if(opinion_id!=null)
		{
			A_opinion opinion=opinionDao.findById(opinion_id);
			request.getSession().setAttribute("opinion", opinion);
		}
		else
		{
			A_opinion ao=(A_opinion) request.getSession().getAttribute("opinion");
			opinion_id=ao.getOpinion_id();
		}
		map.put("opinion_id", opinion_id);
		List<A_reply> list=replyDao.findByCondition(map);
		request.setAttribute("replylist", list);
		String is_excute_notices=(String) request.getSession().getAttribute("is_excute_reply");
		if(is_excute_notices!=null&&is_excute_notices.equals("yes"))
		{
			request.getSession().setAttribute("is_excute_reply", "no");
			request.getRequestDispatcher("reply_list.jsp").forward(request, resp);
		}
		else
		{
			request.getSession().setAttribute("excuteMsg_reply", "");
			request.getRequestDispatcher("reply_list.jsp").forward(request, resp);
		}
	}

	private void add(HttpServletRequest request, HttpServletResponse resp)
			throws IOException {
		//2、判断数据的有效性
		//3、获取数据
		A_reply reply=new A_reply();
		A_adminer adminer=(A_adminer) request.getSession().getAttribute("userInfo");
		int rows=0;
		String errMsg="";
		try{
			reply.setOpinion_id(StringUtils.str2Integer(request.getParameter("opinion_id"), null));
			reply.setAdmin_id(adminer.getAdmin_id());
			reply.setDetails(request.getParameter("details"));
			reply.setStart_date(StringUtils.str2TimestampBySql(request.getParameter("start_date"), null, new Timestamp(new Date().getTime()), false));
			//4、保存数据
			Object[] results=(Object[]) replyDao.saveReply(reply);
			rows=(Integer) results[0];
		}catch(Exception e){
			e.printStackTrace();
			errMsg=e.getMessage();
		}
		if(rows>0){
			request.getSession().setAttribute("is_excute_opinion", "yes");
			request.getSession().setAttribute("excuteMsg_opinion", "回复成功");
		}else{
			request.getSession().setAttribute("excuteMsg_notices", errMsg);
		}
		resp.sendRedirect("opinionServlet");
	}
	
	private void delete(HttpServletRequest request, HttpServletResponse resp)
			throws IOException, ServletException {
		int rows=0;
		String errMsg="";
		String del_list=request.getParameter("del_list");
		if(del_list!=null&&del_list.length()>0)
		{
			String[] delArrStr=del_list.split(";");
			List<Integer> delArrInt=new ArrayList<Integer>();
			for(int i=0;i<delArrStr.length;i++)
			{
				delArrInt.add(StringUtils.str2Integer(delArrStr[i], null));
			}
			for(Integer i:delArrInt)
			{
				rows=replyDao.deleteReply(i);
			}
		}
		else{
			try{
				Integer reply_id=StringUtils.str2Integer(request.getParameter("reply_id"), null);
				if(reply_id!=null){
					//4、保存数据
					rows=replyDao.deleteReply(reply_id);
				}
				else
				{throw new RuntimeException("该记录不存在");}
			}catch(Exception e){
				e.printStackTrace();
				errMsg=e.getMessage();
			}
		}
		if(rows>0){
			request.getSession().setAttribute("is_excute_reply", "yes");
			request.getSession().setAttribute("excuteMsg_reply", "删除成功");
		}else{
			request.getSession().setAttribute("excuteMsg_reply", errMsg);
		}
		request.getRequestDispatcher("replyServlet?opr=list").forward(request, resp);
	}
}
